package com.cheng.chengliving.commodity.controller;

import com.aliyun.oss.*;
import com.cheng.chengliving.commodity.service.CategoryService;
import com.cheng.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @Author: 朱程
 * @Date: 2022/11/13/13:45
 * @Description:
 */

@RestController
@Slf4j
public class TestController {

    //装配OSSClient
    @Resource
    private OSSClient ossClient;

    //上传指定的文件到Bucket
    @RequestMapping("/test2")
    public R testUpload2() throws FileNotFoundException {


            InputStream inputStream = new FileInputStream("D:\\picture\\girl.jpg");
            ossClient.putObject("chengliving-10000","wife.jpg",inputStream);


            ossClient.shutdown();



        return R.ok("上传成功");
    }


    //编写方法-上传指定的文件到指定bucket
//    @RequestMapping("/test")
//    public R testUpload() {
//        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
//        String endpoint = "oss-cn-hangzhou.aliyuncs.com";
//        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
//        String accessKeyId = "LTAI5tGW6QhAGrEtDQ9RfKdU";
//        String accessKeySecret = "RT1RGRxUZHLWSCZDfsoMo5j1yT0X1E";
//        // 填写Bucket名称，例如examplebucket。
//        String bucketName = "chengliving-10000";
//        // 文件上传后的文件名
//        String objectName = "2022-12-12/yaofanjavaer.jpg";
//        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
//        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
//        String filePath= "D:\\picture\\yaofan.jpg";
//
//        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
//        try {
//            InputStream inputStream = new FileInputStream(filePath);
//            // 创建PutObject请求。
//            ossClient.putObject(bucketName, objectName, inputStream);
//        } catch (OSSException oe) {
//            System.out.println("Caught an OSSException, which means your request made it to OSS, "
//                    + "but was rejected with an error response for some reason.");
//            System.out.println("Error Message:" + oe.getErrorMessage());
//            System.out.println("Error Code:" + oe.getErrorCode());
//            System.out.println("Request ID:" + oe.getRequestId());
//            System.out.println("Host ID:" + oe.getHostId());
//        } catch (ClientException ce) {
//            System.out.println("Caught an ClientException, which means the client encountered "
//                    + "a serious internal problem while trying to communicate with OSS, "
//                    + "such as not being able to access the network.");
//            System.out.println("Error Message:" + ce.getMessage());
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            if (ossClient != null) {
//                ossClient.shutdown();
//            }
//        }
//        return R.ok().put("200", "文件上传成功");
//    }

    //编写方法测试 getCascadeCategoryId
    //传入categoryId=1001 => [1,101,1001]

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/test3")
    public R testGetCascadeCategoryId() {
        Long[] cascadeCategoryId = categoryService.getCascadeCategoryId(1001L);
        return R.ok().put("data",cascadeCategoryId);
    }
}
